package megadrum.gui;

import megadrum.*;
import javax.sound.midi.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MidiMonitor extends javax.swing.JPanel implements Receiver, ActionListener {

    public MidiMonitor(MegadrumConfigTool mct, MidiLog ml) {
        parent = mct;
        log = ml;

        initComponents();

        simpleLog = new SimpleMidiLog();
        midiLogPanel.add(simpleLog, "Center");
        viewLog.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        log.setLocationRelativeTo(parent);
        log.setVisible(true);
        log.update(log.getGraphics());
    }

    public void send(MidiMessage event, long time) {
        if (event instanceof ShortMessage) {
            //we only monitor noteOn, CC, and aftertouch events
            switch (event.getStatus() & 0xf0) {
                case 0x90:
                    if (((ShortMessage) event).getData2() != 0) {
                        padLevel.setValue(((ShortMessage) event).getData2());
                        padLevel.setString("" + ((ShortMessage) event).getData2());
                        updateRimSwitch(((ShortMessage) event).getData1(), ((ShortMessage) event).getData2());
                        simpleLog.insertEvent(((ShortMessage) event).getData2());
                    }
                    break;
                case 0xa0:
                    if (((ShortMessage) event).getData2() != 0) {
                        simpleLog.insertEvent(Common.CHOKE);
                    }
                    break;
                case 0xb0:
                    hihatLevel.setValue(((ShortMessage) event).getData2());
                    hihatLevel.setString("" + ((ShortMessage) event).getData2());
                    break;
            }
        }
    }

    public void updateRimSwitch(int receivedNote, int velocity) {
        if (parent.getRimSwitchThreshold() < velocity) {
            int i = parent.getNumberOfMonoPads();
            for (; i < parent.getNumberOfStereoPads(); i++) {
                if (parent.getPad(i).getRimNote1() == receivedNote || parent.getPad(i).getRimNote2() == receivedNote) {
                    rimSwitch.setSelected(true);
                    break;
                }
            }
            if (i == parent.getNumberOfStereoPads()) {
                rimSwitch.setSelected(false);
            }
        }
    }

    public void close() {
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewLog = new javax.swing.JButton();
        javax.swing.JLabel jLabel17 = new javax.swing.JLabel();
        hihatLevel = new javax.swing.JProgressBar();
        javax.swing.JLabel jLabel18 = new javax.swing.JLabel();
        padLevel = new javax.swing.JProgressBar();
        rimSwitch = new javax.swing.JRadioButton();
        javax.swing.JLabel jLabel19 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel20 = new javax.swing.JLabel();
        midiLogPanel = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Midi Monitor"));

        viewLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/megadrum/icons/midilog.png"))); // NOI18N
        viewLog.setText("Midi Log");
        viewLog.setToolTipText("<html>&nbsp;Open midi log window to&nbsp;<br>&nbsp;watch activity on inputs");

        jLabel17.setText("Pad hit velocity");

        hihatLevel.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        hihatLevel.setForeground(new java.awt.Color(0, 204, 51));
        hihatLevel.setMaximum(127);
        hihatLevel.setToolTipText("<html>&nbsp;Hihat pedal position:&nbsp;<br>&nbsp;127 means closed&nbsp;<br>&nbsp;&nbsp;0 means open&nbsp;");
        hihatLevel.setString("0");
        hihatLevel.setStringPainted(true);

        jLabel18.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel18.setText("Closed");

        padLevel.setFont(new java.awt.Font("Tahoma", 0, 10));
        padLevel.setForeground(new java.awt.Color(51, 153, 255));
        padLevel.setMaximum(127);
        padLevel.setToolTipText("<html>&nbsp;Maximum value: 127&nbsp;<br>&nbsp;Minimum value: 0&nbsp;");
        padLevel.setString("0");
        padLevel.setStringPainted(true);

        rimSwitch.setText("Rim hit!");
        rimSwitch.setToolTipText("<html>&nbsp;Rim switch:&nbsp;<br>&nbsp;A red light will turn on when&nbsp;<br>&nbsp;you hit the rim of a pad&nbsp;");
        rimSwitch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/megadrum/icons/rimOff.png"))); // NOI18N
        rimSwitch.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/megadrum/icons/rimOn.png"))); // NOI18N

        jLabel19.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel19.setText("Open");

        jLabel20.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel20.setText(" Hihat Pedal");

        midiLogPanel.setToolTipText("AdadAdasdaDAda");
        midiLogPanel.setMaximumSize(new java.awt.Dimension(166, 175));
        midiLogPanel.setMinimumSize(new java.awt.Dimension(166, 175));
        midiLogPanel.setLayout(new java.awt.BorderLayout());

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, midiLogPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, viewLog, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel19)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel20, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel18))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, hihatLevel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel17)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, rimSwitch)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, padLevel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(midiLogPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel19)
                    .add(jLabel18)
                    .add(jLabel20))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(hihatLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel17)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(padLevel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(rimSwitch)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(viewLog)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar hihatLevel;
    private javax.swing.JPanel midiLogPanel;
    private javax.swing.JProgressBar padLevel;
    private javax.swing.JRadioButton rimSwitch;
    private javax.swing.JButton viewLog;
    // End of variables declaration//GEN-END:variables
    private MegadrumConfigTool parent;
    private MidiLog log;
    private SimpleMidiLog simpleLog;
}
